/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import domain.controller.file.CategoriaController;
import domain.controller.file.RegistroController;
import domain.model.file.Categoria;
import domain.model.file.Registro;
import java.util.Enumeration;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.Ticker;
import util.J2meAppHelper;
import view.components.MObjectList;


/**
 *
 * @author Victorino
 */
public class RegistroForm implements CommandListener, AppForm {

    private MObjectList list = new MObjectList("", List.IMPLICIT);
    private Command cmdDetail = new Command("Detalhes", Command.ITEM, 1);
    private Object backParameter = null;
    private AppForm backForm = new CategoriaRaizForm();
    private Command cmdInitial = new Command("Tela Inicial", Command.BACK, 2);
    private Categoria catSelect = null;

    public RegistroForm() {
        this.list.addCommand(cmdDetail);
        this.list.addCommand(cmdInitial);
        this.list.setCommandListener(this);
    }

    public void commandAction(Command arg0, Displayable arg1) {

        Registro reg = (Registro) list.getSelectedMObject();
        if (list.isBackCmdClick(arg0)) {
            list.forceBack();
            return;
        } else if (cmdInitial.equals(arg0)) {

            J2meAppHelper.getInstance().changeForm(new CategoriaRaizForm());
        } else if (reg != null) {

            Object vo = new RegDetailVO(reg, this.catSelect);
            J2meAppHelper.getInstance().changeForm(new DetalheRegistroForm(), vo);

        } else {

            Alert al = new Alert("Aviso", "ooops ... ocorreu um erro!", null, AlertType.ERROR);
            J2meAppHelper.getInstance().changeForm(al, InicialForm.getInicialForm(), null);
        }
    }

    public Displayable getDisplyable() {
        return list;
    }

    public void initialize(Object parameter) {

        list.deleteAll();
        String title = "Resultados: ";
        if (parameter instanceof String) {
            try {
                Enumeration registros = RegistroController.getInstance().buscarRegistros((String) parameter).elements();
                while (registros.hasMoreElements()) {
                    Registro reg = (Registro) registros.nextElement();
                    list.appendMObject(reg);
                }
                list.setFirstSelected();
                title += " " + J2meAppHelper.getInstance().getUc().getUltimaBusca();
                backForm = new BuscaForm();
                backParameter = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }

        } else if (parameter instanceof Categoria) {

            catSelect = (Categoria) parameter;

            Enumeration regEn = catSelect.getRegistrosIds().elements();

            while (regEn.hasMoreElements()) {
                try {
                    String idd = (String) regEn.nextElement();
                    Registro r = (Registro) RegistroController.getInstance().getRegistro(idd);
                    if (r != null) {
                        list.appendMObject(r);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            title += catSelect.getNome();

            if (catSelect.getCategoriaPaiId() != -1) {
                try {
                    backParameter = CategoriaController.getInstance().getCategoria(catSelect.getCategoriaPaiId());
                    backForm = new CategoriaForm();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            } else {
                backParameter = null;
                backForm = new CategoriaRaizForm();
            }
        }

        list.sortByLabelAlpha();
        
        title += " (" + list.size() + ")";
        list.setTicker(new Ticker(title));

        list.appendBack(backForm, backParameter);
    }
}
